﻿/**
首页自定义
 kaige @2022
 */
layui.define(['layer', 'dropdown', 'util'], function (exports) {
    "use strict";
    var $ = layui.jquery, layer = layui.layer, dropdown = layui.dropdown, util = layui.util;
    var taskboxcss = 'ml-taskbar-task', taskitemcss = 'ml-task-item', task = $('.' + taskboxcss), oframe = $('.content-wrapper');
    //计算多个jq对象的宽度和
    var calSumWidth = function (element) {
        var width = 0;
        $(element).each(function () {
            width += $(this).outerWidth(true);
        });
        return width;
    };
    //滚动到指定选项卡
    var scrollToTab = function (element) {
        var $obj = $('.ml-taskbar-task');
        var left = parseInt($obj.css('margin-left')); //当前left
        var ow = $obj.width(); //总宽
        var visibleWidth = $(".taskbox").width();//可视区宽
        //element是tab(a标签),装有标题那个
        //div.content-tabs > div.page-tabs-content
        var marginLeftVal = calSumWidth($(element).prevAll()),//前面所有tab的总宽度
            marginRightVal = calSumWidth($(element).nextAll());//后面所有tab的总宽度
        //将要滚动的长度
        var scrollVal = 0;
        if ($obj.outerWidth() < visibleWidth) {
            //所有的tab都可以显示的情况
            scrollVal = 0;
        } else if (marginRightVal <= (visibleWidth - $(element).outerWidth(true) - $(element).next().outerWidth(true))) {
            //向右滚动
            //marginRightVal(后面所有tab的总宽度)小于可视区域-(当前tab和下一个tab的宽度)
            if ((visibleWidth - $(element).next().outerWidth(true)) > marginRightVal) {
                scrollVal = marginLeftVal;
                var tabElement = element;
                while ((scrollVal - $(tabElement).outerWidth()) > ($obj.outerWidth() - visibleWidth)) {
                    scrollVal -= $(tabElement).prev().outerWidth();
                    tabElement = $(tabElement).prev();
                }
            }
        } else if (marginLeftVal > (visibleWidth - $(element).outerWidth(true) - $(element).prev().outerWidth(true))) {
            //向左滚动
            scrollVal = marginLeftVal - $(element).prev().outerWidth(true);
        }
        //执行动画
        $obj.animate({
            marginLeft: 0 - scrollVal + 'px'
        }, "fast");
    };

    //滚动条滚动到左边
    var scrollTabLeft = function () {
        var $obj = $('.ml-taskbar-task');
        var left = parseInt($obj.css('margin-left')); //当前left
        var ow = $obj.width(); //总宽
        var visibleWidth = $(".taskbox").width();//可视区宽
        var marginLeftVal = Math.abs(left);
        var scrollVal = 0;
        if (ow < visibleWidth) {
            return false;
        } else {
            var tabElement = $(".ml-task-item:first");
            var offsetVal = 0;
            while ((offsetVal + $(tabElement).outerWidth(true)) <= marginLeftVal) {
                offsetVal += $(tabElement).outerWidth(true);
                tabElement = $(tabElement).next();
            }
            offsetVal = 0;
            if (calSumWidth($(tabElement).prevAll()) > visibleWidth) {
                while ((offsetVal + $(tabElement).outerWidth(true)) < (visibleWidth) && tabElement.length > 0) {
                    offsetVal += $(tabElement).outerWidth(true);
                    tabElement = $(tabElement).prev();
                }
                scrollVal = calSumWidth($(tabElement).prevAll());
            }
        }

        $obj.animate({
            marginLeft: 0 - scrollVal + 'px'
        }, "fast");
    };

    //滚动条滚动到右边
    var scrollTabRight = function () {
        var $obj = $('.ml-taskbar-task');
        var left = parseInt($obj.css('margin-left')); //当前left
        var ow = $obj.width(); //总宽
        var visibleWidth = $(".taskbox").width();//可视区宽
        var marginLeftVal = Math.abs(left);
        var scrollVal = 0;
        if (ow < visibleWidth) {
            return false;
        } else {
            var tabElement = $(".ml-task-item:first");
            var offsetVal = 0;
            while ((offsetVal + $(tabElement).outerWidth(true)) <= marginLeftVal) {
                offsetVal += $(tabElement).outerWidth(true);
                tabElement = $(tabElement).next();
            }
            offsetVal = 0;
            while ((offsetVal + $(tabElement).outerWidth(true)) < (visibleWidth) && tabElement.length > 0) {
                offsetVal += $(tabElement).outerWidth(true);
                tabElement = $(tabElement).next();
            }
            scrollVal = calSumWidth($(tabElement).prevAll());
            if (scrollVal > 0) {
                $obj.animate({
                    marginLeft: 0 - scrollVal + 'px'
                }, "fast");
            }
        }
    };


    var b = function () { };
    b.prototype.config = function () {
        return {
            tabs: {
                id: 'oa_' + 1000 * Math.random(),
                title: '新页面'
            }
        }
    }
    b.prototype.getViewPort = function () {
        var c = window, h = "inner";
        "innerWidth" in window || ((h = "client"), (c = document.documentElement || document.body));
        return { width: c[h + "Width"], height: c[h + "Height"] };
    };

    b.prototype.fixIframeCotent = function (id) {
        var that = this;
        setTimeout(function () {
            var c = $(".main-header");
            c = that.getViewPort().height - c.outerHeight();
            $('#tc_' + id).find(".tab_iframe").css({ height: c, width: "100%" });
        }, 50);
    };
    b.prototype.rightmouse = function (id) {
        var that = this;
        var cdata = [
            { title: '关闭当前', id: 'close_curr' },
            { title: '关闭右侧', id: 'close_right' },
            { title: '关闭左侧', id: 'close_left' },
            { title: '关闭其它', id: 'close_other' },
            { title: '关闭所有', id: 'close_all' },
            { title: '刷新当前', id: 'reload' }
        ];
        if (id == 'oa-welcome') {
            cdata = [{ title: '关闭其它', id: 'close_other' }, { title: '刷新', id: 'reload' }];
        }
        //右键菜单
        var inst = dropdown.render({
            elem: '#task_' + id
            , trigger: 'contextmenu'
            , isAllowSpread: false //禁止菜单组展开收缩
            , id: 'task_rcm_' + id //定义唯一索引
            , data: cdata
            , click: function (obj, othis) {
                switch (obj.id) {
                    case 'close_curr':
                        that.closeTab(id);
                        break;
                    case 'close_right':
                        $('#task_' + id).nextAll().each(function (i, e) {
                            var cid = $(e).attr('tab-id');
                            that.closeTab(cid);
                        });
                        break;
                    case 'close_left':
                        $('#task_' + id).prevAll().each(function (i, e) {
                            var cid = $(e).attr('tab-id');
                            that.closeTab(cid);
                        });
                        break;
                    case 'close_other':
                        var tabid;
                        $('.ml-task-item').each(function (i, e) {
                            var cid = $(e).attr('tab-id');
                            if (cid != id){
                                that.closeTab(cid);
                            }else {
                                tabid = cid;
                            }
                        });
                        if(tabid) that.tabToTop(tabid);
                        break;
                    case 'close_all':
                        $('.ml-task-item').each(function (i, e) {
                            var cid = $(e).attr('tab-id');
                            that.closeTab(cid);
                        });
                        break;
                    case 'reload':
                        $('#iframe_' + id).attr('src', $('#iframe_' + id).attr('src'));
                        break;
                }
            }
        });
    };
    //添加任务栏项
    b.prototype.addTaskItem = function (opt) {
        var that = this;
        var taskItem = '<li id="task_' + opt.id + '" tab-id="' + opt.id + '" class="ml-task-item">' + (opt.icon ? layui.oamenu.getIconHtml(opt.icon) : '') + ' ' + (opt.title ? opt.title : '') + (opt.id == 'oa-welcome' ? '' : '<a class="closetaskitem"><i class="fa fa-window-close"></i></a>') + '</li>';
        task.append(taskItem);
        $('#task_' + opt.id).find('a').on('click', function (e) {
            that.closeTab(opt.id);
        });
        that.rightmouse(opt.id);
    };
    //关闭任务栏选项
    b.prototype.closeTab = function (id) {
        if (id == 'oa-welcome') return;// 首页不允许关闭
        var t1 = $("#task_" + id), t2 = $("#tc_" + id), t3 = $("#iframe_" + id);
        id = t1.prev().attr("tab-id");
        this.tabToTop(id);
        t1.remove();
        t3.attr("src", "about:blank");//先去除框架里的页面，解决某浏览器不会释放内存
        t2.remove();
        var $obj = $('.ml-taskbar-task');
        var ow = $obj.width();
        $obj.css('width', ow - 123);
    };
    //获取个数
    b.prototype.getTabPageNums = function () {
        return task.find("." + taskitemcss).length;
    };
    //任务栏自适应 是否还放得下--更为 放不下时，显示 左右两个点击按钮
    b.prototype.taskAuto = function () {
        var res = true;
        $('.ml-taskbar-task').each(function () {
            var thisWidth = parseInt($(this).prop('scrollWidth'));
            var childWidth = parseInt($(this).children().length * 123);
            //响应式
            if (thisWidth - 123 < childWidth) {
                // layer.msg('任务栏装不下啦，请选关闭一些！');
                res = false;
            }
        });
        return res;
    };
    //当前tab 置前
    b.prototype.tabToTop = function (id) {
        $('.tab_content').hide();
        $('#tc_' + id).show();
        $('.ml-task-item').removeClass('selected');
        $('#task_' + id).addClass('selected');
        scrollToTab('#task_' + id);
    };

    b.prototype.toggletab = function (id) {
        var that = this;
        $('#task_' + id).on('click', function (e) {
            var id1 = $(this).attr('tab-id');
            that.tabToTop(id1);
        });
    }

    //添加TAB内容区
    b.prototype.addTabs = function (a) {
        var that = this;
        a = $.extend(that.config().tabs, a);
        if ($("#tc_" + a.id).length > 0) {
            that.tabToTop(a.id);
            return;
        }
        if (!that.taskAuto()) {
            var $obj = $('.ml-taskbar-task');
            var ow = $obj.width();
            $obj.css('width', ow + 123);
        }
        that.addTaskItem(a);
        var s = '<div class="tab_content" id="tc_' + a.id + '"><iframe src="' + a.url + '" frameborder="no" id="iframe_' + a.id + '" class="tab_iframe"></iframe></div>';
        oframe.append(s);

        that.fixIframeCotent(a.id);
        that.tabToTop(a.id);
        that.toggletab(a.id);
    };

    b.prototype.init = function () {
        var that = this;
        this.addTabs({
            id: 'oa-welcome',
            title: false,
            icon: 'fa fa-home',
            url: kaige_page.welcome
        });

        //修改密码
        $("#edit-pass").on("click", function () {
            that.addTabs({
                id: 'oa_editpwd_box',
                title: '修改密码',
                url: szoa.client.path + '/pages/sys/user/information.html'
            });
        });
        $(".overleftbtn").click(function () {
            scrollTabLeft();
        });
        $(".overrightbtn").click(function () {
            scrollTabRight();
        });

        dropdown.render({
            elem: '#overmorebtn'
            , trigger: 'mousedown'
            , data: [
                { title: '关闭当前', id: 'close_curr' },
                { title: '关闭右侧', id: 'close_right' },
                { title: '关闭左侧', id: 'close_left' },
                { title: '关闭其它', id: 'close_other' },
                { title: '关闭所有', id: 'close_all' },
                { title: '刷新当前', id: 'reload' }
            ]
            , align: 'right'
            , click: function (obj, othis) {
                var $obj = $('.ml-taskbar-task').find('.ml-task-item.selected');
                var tabid = $obj.attr('tab-id');
                if (tabid) {
                    switch (obj.id) {
                        case 'close_curr':
                            if (tabid == 'oa-welcome') { return false; }
                            that.closeTab(tabid);
                            break;
                        case 'close_right':
                            $('#task_' + tabid).nextAll().each(function (i, e) {
                                var cid = $(e).attr('tab-id');
                                that.closeTab(cid);
                            });
                            break;
                        case 'close_left':
                            if(tabid =='oa-welcome'){return false;}
                            $('#task_' + tabid).prevAll().each(function (i, e) {
                                var cid = $(e).attr('tab-id');
                                that.closeTab(cid);
                            });
                            break;
                        case 'close_other':
                            var $now_obj;
                            $('.ml-task-item').each(function (i, e) {
                                var cid = $(e).attr('tab-id');
                                if (cid != tabid) that.closeTab(cid);
                            });
                            that.tabToTop(tabid);
                            break;
                        case 'close_all':
                            $('.ml-task-item').each(function (i, e) {
                                var cid = $(e).attr('tab-id');
                                that.closeTab(cid);
                            });
                            break;
                        case 'reload':
                            $('#iframe_' + tabid).attr('src', $('#iframe_' + tabid).attr('src'));
                            break;
                    }
                }
            }
        });
    };

    b.prototype.open = function (options) {
        console.log(options);
        var objId = 'szoa_open_id_' + options.id;
        var obj = $('#' + objId);
        if (obj.length > 0) {
            return false;
        }
        //打开窗口
        layer.open({
            id: objId,
            type: options.type || 2,
            title: options.title || '',
            content: options.content || '',
            area: options.area || ['90%', '90%'],
            offset: options.offset || 'auto',
            anim: options.anim || 0,
            shade: options.shade || 0,
            maxmin: options.maxmin || false,   //允许最大最小化
            moveOut: options.moveOut || false,  //允许拖出窗外
            skin: options.skin || '', //窗口皮肤
            //销毁回调
            end: options.end || function () { },
            //拖动回调
            moveEnd: function (layero) { },
            //打开回调
            success: function (layero, index) {
                // 成功后回调
                if (typeof successCall === 'function') successCall(layero, index);
            },
            //关闭回调
            cancel: function (index, layero) { }
        });
    };

    var oawin = new b();
    exports('oawin', oawin);
});